package com.yunhe.fountain.service.impl;

import com.yunhe.common.exception.ArgumentErrorException;
import com.yunhe.fountain.dao.FountainControlDao;
import com.yunhe.fountain.domain.Fountain;
import com.yunhe.fountain.scoket.CardFlow;
import com.yunhe.fountain.service.FountainControlService;
import lombok.extern.log4j.Log4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;

/**
 * @author zhengbo
 * @date 2021/7/8 10:25
 */
@Service
@Log4j
public class FountainControlServiceImpl implements FountainControlService {

    @Autowired
    private FountainControlDao fountainControlDao;

    /**
     * 发送指令
     * @param fountain
     * @return
     * connection_success       连接成功
     * connection_fail          建立连接失败
     * carry_on_success         继续成功
     * carry_on_fail            继续失败
     * stop_fail                停止失败
     * stop_success             停止成功
     * start_program_fail       开启节目失败
     * start_program_success    开启节目成功
     * issue_order_fail         下发指令失败
     * issue_order_success      下发指令成功
     */
    @Override
    public String sendCommand(Fountain fountain) {

        String returnValue;
        //建立连接失败
        String connectionFail = "connection_fail";
        try {
            //如果传过来的节目与之前保存的节目相同，则是发送继续播放指令
            Fountain existFountain = fountainControlDao.getFountain(fountain.getFountainIp(), fountain.getFountainPort());
            if (existFountain != null
                    && StringUtils.isNotEmpty(existFountain.getFountainCurrentShow())
                    && StringUtils.isNotEmpty(fountain.getFountainCurrentShow())
                    && StringUtils.isEmpty(fountain.getCommand())
                    && existFountain.getFountainCurrentShow().equals(fountain.getFountainCurrentShow())) {
                returnValue = CardFlow.issueAnOrder(fountain.getFountainIp(), Integer.parseInt(fountain.getFountainPort()), "0200");
                return returnValue;
            }

            if (StringUtils.isEmpty(fountain.getCommand())) {
                fountain.setCommand(fountain.getFountainCurrentShow());
            }

            returnValue = CardFlow.issueAnOrder(fountain.getFountainIp(), Integer.parseInt(fountain.getFountainPort()), fountain.getCommand());

            //开启节目成功start_program_success 更新节目 更新目前节目状态为播放中
            if ("start_program_success".equals(returnValue)) {
                fountain.setProgramStatus("0");
            }

            //开启节目失败 不进行操作

            //继续成功carry_on_success 更新目前节目状态 为播放中
            if ("carry_on_success".equals(returnValue)) {
                fountain.setProgramStatus("0");
            }

            //继续失败 不进行操作

            //停止成功stop_success 更新目前节目状态 为已停止
            if ("stop_success".equals(returnValue)) {
                fountain.setProgramStatus("1");
            }

            //停止失败 不进行操作

            //连接失败
            if (connectionFail.equals(returnValue)) {
                fountain.setProgramStatus("1");
                fountain.setFountainStatus("1");
            }

        } catch (IOException e) {
            returnValue = "issue_order_fail";
        } catch (ArgumentErrorException e) {
            returnValue = e.getMessage();
            //连接失败
            if (connectionFail.equals(e.getMessage())) {
                fountain.setProgramStatus("1");
            }
        }

        saveOrUpdate(fountain);

        return returnValue;
    }

    @Override
    public Fountain establishConnection(Fountain fountain) throws IOException {
        Fountain existFountain = fountainControlDao.getFountain(fountain.getFountainIp(), fountain.getFountainPort());
        String result;
        //建立连接成功
        String connectionSuccess = "connection_success";
        result = CardFlow.establishConnection(fountain.getFountainIp(), Integer.parseInt(fountain.getFountainPort()));
        if (connectionSuccess.equals(result)) {
            fountain.setFountainStatus("0");
            //喷泉存在的话 查询喷泉的当前节目 并返回
            if (existFountain != null) {
                existFountain.setFountainStatus("0");
                fountain = existFountain;
            }
        } else {
            fountain.setFountainStatus("1");
            //喷泉存在的话 查询喷泉的当前节目 并返回
            if (existFountain != null) {
                existFountain.setFountainStatus("1");
                fountain = existFountain;
            }
        }
        saveOrUpdate(fountain);
        return fountain;
    }

    @Override
    public List<Fountain> getFountainControl() {
        return fountainControlDao.getFountainList();
    }

    private void saveOrUpdate(Fountain fountain) {
        Fountain existFountain = fountainControlDao.getFountain(fountain.getFountainIp(), fountain.getFountainPort());
        if (existFountain != null) {
            fountainControlDao.updateFountain(fountain);
        } else {
            fountainControlDao.addFountain(fountain);
        }
    }
}
